home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectSound / DeferredEffects / frmFX.frm next >
Text File  |  2001-10-08  |  18KB  |  504 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmEffects 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Audio Effects using DirectSound Buffers"
  6.    ClientHeight    =   4965
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4740
  10.    Icon            =   "frmFX.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   4965
  14.    ScaleWidth      =   4740
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.Timer tmrUpdate 
  17.       Interval        =   50
  18.       Left            =   6180
  19.       Top             =   1620
  20.    End
  21.    Begin VB.CheckBox chkLoop 
  22.       Caption         =   "Loop Sound"
  23.       Height          =   315
  24.       Left            =   840
  25.       TabIndex        =   7
  26.       Top             =   4500
  27.       Width           =   1455
  28.    End
  29.    Begin VB.CommandButton cmdStop 
  30.       Caption         =   "&Stop"
  31.       Height          =   375
  32.       Left            =   3600
  33.       TabIndex        =   6
  34.       Top             =   4500
  35.       Width           =   1095
  36.    End
  37.    Begin VB.CommandButton cmdPlay 
  38.       Caption         =   "&Play"
  39.       Height          =   375
  40.       Left            =   2400
  41.       TabIndex        =   5
  42.       Top             =   4500
  43.       Width           =   1095
  44.    End
  45.    Begin VB.Frame fraEffects 
  46.       Caption         =   "Effects Information"
  47.       Height          =   3615
  48.       Left            =   120
  49.       TabIndex        =   1
  50.       Top             =   780
  51.       Width           =   4515
  52.       Begin VB.CommandButton cmdApply 
  53.          Caption         =   "Apply Effects"
  54.          Height          =   315
  55.          Left            =   2460
  56.          TabIndex        =   12
  57.          Top             =   3180
  58.          Width           =   1875
  59.       End
  60.       Begin VB.CommandButton cmdRemove 
  61.          Height          =   285
  62.          Left            =   2400
  63.          MaskColor       =   &H000000FF&
  64.          Picture         =   "frmFX.frx":0442
  65.          Style           =   1  'Graphical
  66.          TabIndex        =   11
  67.          Top             =   1920
  68.          UseMaskColor    =   -1  'True
  69.          Width           =   315
  70.       End
  71.       Begin VB.CommandButton cmdAdd 
  72.          Height          =   285
  73.          Left            =   2040
  74.          MaskColor       =   &H000000FF&
  75.          Picture         =   "frmFX.frx":0984
  76.          Style           =   1  'Graphical
  77.          TabIndex        =   10
  78.          Top             =   1920
  79.          UseMaskColor    =   -1  'True
  80.          Width           =   315
  81.       End
  82.       Begin VB.ListBox lstUse 
  83.          Height          =   840
  84.          Left            =   120
  85.          TabIndex        =   9
  86.          Top             =   2220
  87.          Width           =   4275
  88.       End
  89.       Begin VB.ListBox lstAvail 
  90.          Height          =   840
  91.          ItemData        =   "frmFX.frx":0EC6
  92.          Left            =   120
  93.          List            =   "frmFX.frx":0EE2
  94.          TabIndex        =   8
  95.          Top             =   1020
  96.          Width           =   4275
  97.       End
  98.       Begin VB.TextBox txtFile 
  99.          Height          =   285
  100.          Left            =   120
  101.          Locked          =   -1  'True
  102.          TabIndex        =   3
  103.          Text            =   "No file loaded..."
  104.          Top             =   480
  105.          Width           =   3975
  106.       End
  107.       Begin VB.CommandButton cmdBrowse 
  108.          Caption         =   "..."
  109.          Height          =   285
  110.          Left            =   4140
  111.          TabIndex        =   2
  112.          ToolTipText     =   "Open a new audio file..."
  113.          Top             =   480
  114.          Width           =   315
  115.       End
  116.       Begin VB.Label lbl 
  117.          BackStyle       =   0  'Transparent
  118.          Caption         =   "Available Effects"
  119.          Height          =   195
  120.          Index           =   3
  121.          Left            =   120
  122.          TabIndex        =   15
  123.          Top             =   780
  124.          Width           =   1215
  125.       End
  126.       Begin VB.Label lbl 
  127.          BackStyle       =   0  'Transparent
  128.          Caption         =   "Effects in use"
  129.          Height          =   195
  130.          Index           =   2
  131.          Left            =   120
  132.          TabIndex        =   14
  133.          Top             =   1980
  134.          Width           =   1215
  135.       End
  136.       Begin VB.Label lbl 
  137.          BackStyle       =   0  'Transparent
  138.          Caption         =   "Available Effects"
  139.          Height          =   195
  140.          Index           =   1
  141.          Left            =   180
  142.          TabIndex        =   13
  143.          Top             =   600
  144.          Width           =   1215
  145.       End
  146.       Begin VB.Label lbl 
  147.          BackStyle       =   0  'Transparent
  148.          Caption         =   "Currently loaded sound file:"
  149.          Height          =   195
  150.          Index           =   0
  151.          Left            =   120
  152.          TabIndex        =   4
  153.          Top             =   240
  154.          Width           =   4515
  155.       End
  156.    End
  157.    Begin MSComDlg.CommonDialog cdlOpen 
  158.       Left            =   300
  159.       Top             =   3720
  160.       _ExtentX        =   847
  161.       _ExtentY        =   847
  162.       _Version        =   393216
  163.    End
  164.    Begin VB.Label lbl 
  165.       BackStyle       =   0  'Transparent
  166.       Caption         =   "Audio Effects using Defered loading DirectSoundBuffers.  This allows you to check the status of effects before playing."
  167.       Height          =   615
  168.       Index           =   4
  169.       Left            =   660
  170.       TabIndex        =   0
  171.       Top             =   60
  172.       Width           =   3195
  173.    End
  174.    Begin VB.Image Image1 
  175.       Height          =   480
  176.       Left            =   120
  177.       Picture         =   "frmFX.frx":0F33
  178.       Top             =   180
  179.       Width           =   480
  180.    End
  181. End
  182. Attribute VB_Name = "frmEffects"
  183. Attribute VB_GlobalNameSpace = False
  184. Attribute VB_Creatable = False
  185. Attribute VB_PredeclaredId = True
  186. Attribute VB_Exposed = False
  187. Option Explicit
  188.  
  189. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  190. '
  191. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  192. '
  193. '  File:       frmFX.frm
  194. '
  195. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  196. 'API declare for windows folder
  197. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  198.  
  199. Private Const mlMaxEffects As Long = 20
  200. 'Private declares for our DirectX objects
  201. Private dx As DirectX8
  202. Private ds As DirectSound8
  203. Private dsb As DirectSoundSecondaryBuffer8
  204. Private mlEffectKey As Long
  205.  
  206. Private Sub cmdAdd_Click()
  207.     If lstAvail.ListIndex = -1 Then 'Nothing is selected
  208.         MsgBox "Please select an available effect before attempting to add it.", vbOKOnly Or vbInformation, "Nothing selected."
  209.         Exit Sub
  210.     End If
  211.     If lstUse.ListCount >= mlMaxEffects Then
  212.         MsgBox "You cannot add more than " & CStr(mlMaxEffects) & " effects in this sample.", vbOKOnly Or vbInformation, "No more effects."
  213.         Exit Sub
  214.     End If
  215.     'Add this item to our list of effects
  216.     lstUse.AddItem lstAvail.List(lstAvail.ListIndex) & " - Unallocated"
  217. End Sub
  218.  
  219. Private Sub cmdApply_Click()
  220.     On Local Error GoTo NoFX
  221.     Dim DSEffects() As DSEFFECTDESC
  222.     Dim lResults() As Long
  223.     Dim lTempEffect As Long
  224.     Dim lCount As Long
  225.     
  226.     'Do we have a sound buffer
  227.     If dsb Is Nothing Then
  228.         MsgBox "You must first load a wave file into a sound buffer before you can apply effects to it.", vbOKOnly Or vbInformation, "No buffer"
  229.         Exit Sub
  230.     End If
  231.     'Yup, now is there a sound already playing?
  232.     If (dsb.GetStatus And DSBSTATUS_PLAYING) = DSBSTATUS_PLAYING Then
  233.         MsgBox "Stop the currently playing sound before adding effects.", vbOKOnly Or vbInformation, "Sound is playing"
  234.         Exit Sub
  235.     End If
  236.     'Yes we do, do we have effects selected?
  237.     If lstUse.ListCount = 0 Then
  238.         If MsgBox("Do you want to turn off effects for this buffer?", vbYesNo Or vbQuestion, "No effects") = vbYes Then
  239.             'Calling SetFX with a count of 0 removes the effects from the buffer
  240.             dsb.SetFX 0, DSEffects, lResults
  241.             Exit Sub
  242.         Else
  243.             MsgBox "You must first select some effects to use.", vbOKOnly Or vbInformation, "No effects"
  244.             Exit Sub
  245.         End If
  246.     End If
  247.     'Ok, let's apply our effects info here
  248.     
  249.     'First get an array of effects structs the right size
  250.     ReDim DSEffects(lstUse.ListCount - 1)
  251.     ReDim lResults(lstUse.ListCount - 1)
  252.     
  253.     For lCount = 0 To lstUse.ListCount - 1
  254.         Select Case Left$(LCase(lstUse.List(lCount)), InStr(lstUse.List(lCount), " ") - 1)
  255.         Case "distortion"
  256.             lTempEffect = lTempEffect + (lCount + &H10)
  257.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_DISTORTION
  258.         Case "echo"
  259.             lTempEffect = lTempEffect + (lCount + &H20)
  260.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_ECHO
  261.         Case "chorus"
  262.             lTempEffect = lTempEffect + (lCount + &H40)
  263.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_CHORUS
  264.         Case "flanger"
  265.             lTempEffect = lTempEffect + (lCount + &H80)
  266.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_FLANGER
  267.         Case "compressor"
  268.             lTempEffect = lTempEffect + (lCount + &H100)
  269.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_COMPRESSOR
  270.         Case "gargle"
  271.             lTempEffect = lTempEffect + (lCount + &H200)
  272.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_GARGLE
  273.         Case "parameq"
  274.             lTempEffect = lTempEffect + (lCount + &H400)
  275.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_PARAMEQ
  276.         Case "wavesreverb"
  277.             lTempEffect = lTempEffect + (lCount + &H800)
  278.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_WAVES_REVERB
  279.         End Select
  280.     Next
  281.     If mlEffectKey <> lTempEffect Then 'They don't match, set the fx.
  282.         dsb.SetFX lstUse.ListCount, DSEffects, lResults
  283.         'Now we can acquire the resources needed for these effects.
  284.         dsb.AcquireResources 0, lResults
  285.         Dim sNewItem As String
  286.         For lCount = 0 To lstUse.ListCount - 1
  287.             sNewItem = Left$(lstUse.List(lCount), InStr(lstUse.List(lCount), " ") - 1)
  288.             Select Case lResults(lCount)
  289.             Case DSFXR_FAILED
  290.                 lstUse.List(lCount) = sNewItem & " - Failed"
  291.             Case DSFXR_LOCHARDWARE
  292.                 lstUse.List(lCount) = sNewItem & " - Hardware"
  293.             Case DSFXR_LOCSOFTWARE
  294.                 lstUse.List(lCount) = sNewItem & " - Software"
  295.             Case DSFXR_UNALLOCATED
  296.                 lstUse.List(lCount) = sNewItem & " - Unallocated"
  297.             Case DSFXR_UNKNOWN
  298.                 lstUse.List(lCount) = sNewItem & " - Unknown"
  299.             Case DSFXR_PRESENT
  300.                 lstUse.List(lCount) = sNewItem & " - Present"
  301.             End Select
  302.         Next
  303.     End If
  304.     mlEffectKey = lTempEffect
  305.     Exit Sub
  306.     
  307. NoFX:
  308.     MsgBox "This set of effects could not be set on this audio file.", vbOKOnly Or vbInformation, "Cannot set"
  309. End Sub
  310.  
  311. Private Sub cmdBrowse_Click()
  312.     Static sCurDir As String
  313.     Dim desc As DSBUFFERDESC
  314.     
  315.     'We want to open a file now
  316.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  317.     cdlOpen.Filter = "Wave Files (*.wav)|*.wav"
  318.     cdlOpen.FileName = vbNullString
  319.     If sCurDir = vbNullString Then
  320.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  321.         Dim sWindir As String
  322.         sWindir = Space$(255)
  323.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  324.             'We couldn't get the windows folder for some reason, use the c:\
  325.             cdlOpen.InitDir = "C:\"
  326.         Else
  327.             Dim sMedia As String
  328.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  329.             If Right$(sWindir, 1) = "\" Then
  330.                 sMedia = sWindir & "Media"
  331.             Else
  332.                 sMedia = sWindir & "\Media"
  333.             End If
  334.             'We are trying to find the windows\media directory.  If it
  335.             'doesn't exist, then use the windows folder as a default
  336.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  337.                 cdlOpen.InitDir = sMedia
  338.             Else
  339.                 cdlOpen.InitDir = sWindir
  340.             End If
  341.         End If
  342.     Else
  343.         'No need to move folders.  Stay where they picked the last file
  344.         cdlOpen.InitDir = sCurDir
  345.     End If
  346.     On Local Error GoTo ClickedCancel
  347.     cdlOpen.CancelError = True
  348.     cdlOpen.ShowOpen   ' Display the Open dialog box
  349.     
  350.     'Save the current information
  351.     sCurDir = GetFolder(cdlOpen.FileName)
  352.             
  353.     On Local Error GoTo NoLoadSegment
  354.     'Before we load the buffer stop one if it's playing
  355.     If Not (dsb Is Nothing) Then If dsb.GetStatus = DSBSTATUS_PLAYING Then dsb.Stop
  356.     'We need to set the CTRLFX flag so we can control the effects on this object
  357.     'We pass the LOCDEFER flag so we can acquire the
  358.     'resources for the effects before we play them
  359.     desc.lFlags = DSBCAPS_CTRLFX Or DSBCAPS_LOCDEFER
  360.     'Now let's load the segment
  361.     Set dsb = ds.CreateSoundBufferFromFile(cdlOpen.FileName, desc)
  362.     mlEffectKey = 0
  363.     txtFile.Text = cdlOpen.FileName
  364.     
  365.     Exit Sub
  366. NoLoadSegment:
  367.     If Err.Number = DSERR_BUFFERTOOSMALL Then 'This buffer isn't big enough to control effects on
  368.         MsgBox "This file isn't long enough to control effects.  Please choose a longer audio file.", vbOKOnly Or vbCritical, "Couldn't load"
  369.     Else 'Some other error
  370.         MsgBox "Couldn't load this file", vbOKOnly Or vbCritical, "Couldn't load"
  371.     End If
  372. ClickedCancel:
  373. End Sub
  374.  
  375. Private Sub cmdPlay_Click()
  376.     If dsb Is Nothing Then
  377.         MsgBox "You must first load a wave file into a sound buffer before you can play it.", vbOKOnly Or vbInformation, "No buffer"
  378.         Exit Sub
  379.     End If
  380.     dsb.Play chkLoop.Value
  381.     EnablePlayUI False
  382. End Sub
  383.  
  384. Private Sub cmdRemove_Click()
  385.     If lstUse.ListIndex = -1 Then 'Nothing is selected
  386.         MsgBox "Please select an effect that's being used before attempting to remove it.", vbOKOnly Or vbInformation, "Nothing selected."
  387.         Exit Sub
  388.     End If
  389.     'Add this item to our list of effects
  390.     lstUse.RemoveItem lstUse.ListIndex
  391. End Sub
  392.  
  393. Private Sub cmdSave_Click()
  394.     On Error GoTo ClickedCancel
  395.     With cdlOpen
  396.         .InitDir = GetFolder(txtFile.Text)
  397.         .FileName = txtFile.Text
  398.         .CancelError = True
  399.         .ShowSave
  400.         dsb.SaveToFile .FileName
  401.     End With
  402.     Exit Sub
  403.  
  404. ClickedCancel:
  405. End Sub
  406.  
  407. Private Sub cmdStop_Click()
  408.     If dsb Is Nothing Then
  409.         MsgBox "You must first load a wave file into a sound buffer before you can stop it.", vbOKOnly Or vbInformation, "No buffer"
  410.         Exit Sub
  411.     End If
  412.     dsb.Stop
  413.     'Stop doesn't reset the current position
  414.     dsb.SetCurrentPosition 0
  415.     EnablePlayUI True
  416. End Sub
  417.  
  418. Private Sub Form_Load()
  419.     EnablePlayUI True
  420.     InitDSound
  421. End Sub
  422.  
  423. Private Sub Form_Unload(Cancel As Integer)
  424.     CleanupDSound
  425. End Sub
  426.  
  427. Private Sub InitDSound()
  428.     
  429.     On Error GoTo FailedInit
  430.     Set dx = New DirectX8
  431.     'Create our default DirectSound object
  432.     Set ds = dx.DirectSoundCreate(vbNullString)
  433.     ds.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL
  434.     Exit Sub
  435.     
  436. FailedInit:
  437.     MsgBox "Could not initialize DirectSound." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  438.     Unload Me
  439.     
  440. End Sub
  441.  
  442. Private Sub CleanupDSound()
  443.     'Let's clean up now
  444.     If Not dsb Is Nothing Then
  445.         'iF we are playing our file, stop it
  446.         If dsb.GetStatus = DSBSTATUS_PLAYING Then dsb.Stop
  447.         'Destroy our objects
  448.         Set dsb = Nothing
  449.     End If
  450.     Set ds = Nothing
  451.     Set dx = Nothing
  452. End Sub
  453.  
  454. Private Function GetFolder(ByVal sFile As String) As String
  455.     Dim lCount As Long
  456.     
  457.     For lCount = Len(sFile) To 1 Step -1
  458.         If Mid$(sFile, lCount, 1) = "\" Then
  459.             GetFolder = Left$(sFile, lCount)
  460.             Exit Function
  461.         End If
  462.     Next
  463.     GetFolder = vbNullString
  464. End Function
  465.  
  466. Private Sub lstAvail_DblClick()
  467.     'Double clicking should be the same as clicking the 'Add' button
  468.     cmdAdd_Click
  469. End Sub
  470.  
  471. Private Sub lstUse_DblClick()
  472.     'Double clicking should be the same as clicking the 'Remove' button
  473.     cmdRemove_Click
  474. End Sub
  475.  
  476. Private Sub EnablePlayUI(ByVal fEnable As Boolean)
  477.     On Error Resume Next
  478.     If fEnable Then
  479.         chkLoop.Enabled = True
  480.         cmdPlay.Enabled = True
  481.         cmdStop.Enabled = False
  482.         cmdBrowse.Enabled = True
  483.         cmdPlay.SetFocus
  484.     Else
  485.         chkLoop.Enabled = False
  486.         cmdPlay.Enabled = False
  487.         cmdStop.Enabled = True
  488.         cmdBrowse.Enabled = False
  489.         cmdStop.SetFocus
  490.     End If
  491. End Sub
  492.  
  493. Private Sub tmrUpdate_Timer()
  494.     If Not (dsb Is Nothing) Then
  495.         If (dsb.GetStatus And DSBSTATUS_PLAYING) <> DSBSTATUS_PLAYING Then
  496.             If cmdPlay.Enabled = False Then
  497.                 EnablePlayUI True
  498.             End If
  499.         End If
  500.     End If
  501. End Sub
  502.  
  503.  
  504.